<?php
namespace app\shop\home;

use app\shop\model\Banner;
use app\shop\model\Artwork as ArtworkModel;
use app\shop\model\Artist;
use app\shop\model\Love;
use app\shop\model\Car;
use app\shop\model\ArtworkGroup;
use app\shop\model\ArtworkLog;
use app\admin\model\Attachment;
/**
 * 艺术家
 */
class Artwork extends Common
{
    public function group()
    {
    	switch (input('get.order')) {
    		case '2':
    			$order = 'create_time asc';
    			break;
    		case '3':
    			$order = 'auction_price desc';
    			break;
    		default:
    			$order = 'create_time';
    			break;
    	}
    	$where = [];
    	if(!empty(request()->param('keyword'))){
    		$where['title'] = ['like','%'.request()->param('keyword').'%'];
        }
    	if(!empty(request()->param('id'))){
    		$where['group_id'] = request()->param('id');
    	}

     	$artwork = ArtworkModel::with('getartist')->order($order)->where($where)->paginate(10);
     	$artistIn = [];
     	foreach ($artwork as $k => &$v) {
            $ins = [];
     		$artistIn[] = $v['artist_id'];
     		$in = explode(',', $v['photos']);
            for($i = 0; $i <count($in); $i++){
               $ins[] = Attachment::where('id',$in[$i])->value('path');
            }
             $v['img'] = $ins;
     	}
    
     	$artist = Artist::with('getimgpath')->order('group')->where('id','in',$artistIn)->select();

        $this->assign('artist', $artist);
        $this->assign('artworks', $artwork);
        if(isMobile()){
            return $this->fetch('Mobile/Artwork/group');
        }else{
            return $this->fetch();
        }
    }

    public function detail()
    {

    	$artwork = ArtworkModel::with(['getartist.getimgpath','getgroup'])->where('id',request()->param('id'))->find();
//    	pr($artwork->toArray());exit;
        $images = explode(',', $artwork['photos']);
        $imgs = Attachment::where('id','in',$images)->field('path')->select();

        //获取该用户的作品
        $artworks = ArtworkModel::with(['getartist.getimgpath'])->where('auction_id',$artwork['auction_id'])->select();
        foreach ($artworks as $k => &$v) {
            $ins = [];
            $in = explode(',', $v['photos']);
            for($i = 0; $i <count($in); $i++){
               $ins[] = Attachment::where('id',$in[$i])->value('path');
            }
            $artworks[$k]['img'] = $ins;
        }
        $love = 0;
        if(isset(session("auction_user")['id']) && session("auction_user")['id']){
            $love = Love::where(['user_id'=>session("auction_user")['id'],'artwork_id'=>request()->param('id')])->value('id');
        }
//        var_dump($artworks);die;
        ArtworkModel::where(['id'=>request()->param('id')])->setInc('read_count',1);

        $this->assign('list', $artworks);
    	$this->assign('artwork', $artwork);
        $this->assign('imgs', $imgs);
        $this->assign('love', $love);

        if(isMobile()){
            if($artwork['type'] == 1){
                return $this->fetch('Mobile/Artwork/detail');
            }else{
                return $this->fetch('Mobile/Artwork/detail_pai');
            }
        }else{
             if($artwork['type'] == 1){
                return $this->fetch();
            }else{
                return $this->fetch('artwork/detail_pai');
            }
        }
    }

    public function ajaxFollow(){
        $data = input('post.');
        $res['code'] = 0;
        $res['message'] = '未知错误';
        if(isset(session("auction_user")['id']) && session("auction_user")['id']){
            if(isset($data['del']) && $data['del']){
                $del = Love::where(['user_id'=>session("auction_user")['id'],'artwork_id'=>request()->param('id')])->delete();
                if($del){
                    ArtworkModel::where(['id'=>request()->param('id')])->setDec('love_count',1);
                    $res['code'] = 1;
                    $res['message'] = '已取消收藏';
                }else{
                    $res['message'] ='操作失败';
                }
            }else{
                $save = Love::insert(['user_id'=>session("auction_user")['id'],'artwork_id'=>request()->param('id')]);
                if($save){
                    ArtworkModel::where(['id'=>request()->param('id')])->setInc('love_count',1);
                    $res['code'] = 1;
                    $res['message'] = '已成功加入收藏';
                }
            }
        }else{
            $res['message'] = '请先登录';
        }
        echo json_encode($res);exit;
    }
    public function addcar (){
        $user = session("auction_user");
        if(!$user){
            $this->error('请先登录');
        }
        $artwork = ArtworkModel::where('id',request()->param('artwork_id'))->find();
        if(!$artwork){
            $this->error('商品信息错误');
        }
        $data['artwork_id'] = $artwork->id;
        $data['user_id'] = $user['id'];
        $data['create_time'] = time();
        $old = Car::where(['artwork_id'=>request()->param('artwork_id'),'user_id'=>$user['id']])->find();
        if($old){
            $this->error('购物车中已有该商品');
        }
        $res = Car::insert($data);
        if(!$res){
            $this->error('添加购物车失败，请稍后重试');
        }
        return $this->fetch();
    }

    public function cart(){
        $user = session("auction_user");
        if(!$user){
            $this->error('请先登录');
        }
        $datalist = Car::where(['user_id'=>$user['id']])->column('id,artwork_id');
        $where['id'] = ['in',$datalist];
        $artwork = ArtworkModel::with('getartist')->where($where)->select();
        $artistIn = [];
        $count = 0;
        foreach ($artwork as $k => &$v) {
            $ins = [];
            $artistIn[] = $v['artist_id'];
            $in = explode(',', $v['photos']);
            for($i = 0; $i <count($in); $i++){
                $ins[] = Attachment::where('id',$in[$i])->value('path');
            }
            $v['img'] = $ins;
            $artwork[$k] = $v;
            $count += $v['price'];
        }
        $datalist = $artwork;
        $this->assign('count',$count);
        $this->assign('datalist',$datalist);
//        pr($artwork[0]->toArray());exit;
        if(isMobile()){
            return $this->fetch('Mobile/Artwork/cart');
        }else{
            return $this->fetch();
        }
    }
    public function delCart(){
//        echo request()->param('id');exit;
        $user = session("auction_user");
        if(!$user){
            exit;
        }
        $datalist = Car::where(['user_id'=>$user['id'],'artwork_id'=>request()->param('id')])->delete();
        echo $datalist;
    }

    public function artworkLog()
    {
        $productId = request()->param('artwork_id');

        $price = ArtworkLog::where('artwork_id',$productId)->field('*')->order('id desc')->find();
        if(empty($price)){
            echo json_encode(['code'=>1,'data'=>0]);exit;
        }else{
            echo json_encode(['code'=>0,'data'=>$price['price']]);exit;
        }
        
    }

}